#include "UserFoodOrderListWindow.h"
#include "ui_UserFoodOrderListWindow.h"

UserFoodOrderListWindow::UserFoodOrderListWindow(QWidget *parent) :
    QListWidget(parent),
    ui(new Ui::UserFoodOrderListWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("我的订单");
    /**
      * @leiteorz
      * 加载订单列表
      */
    FoodOrderTable food_order_table;
    CurrentUser current_user;

    FoodOrder *foodOrder = new FoodOrder[100];
    User cu = current_user.queryCurrentUser();
    foodOrder = food_order_table.queryFoodOrderByAccount(cu.account);   //查询该账户下所有订单

    int i = 0;

    while(foodOrder[i].bill != ""){
        /**
          * @leiteorz
          * 绘制ListItem
          */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(789,89));
        OrderItem *orderItem = new OrderItem();
        orderItem->init(foodOrder[i].id);
        this->setItemWidget(item,orderItem);

        connect(orderItem,SIGNAL(queryOrder(FoodOrder)),this,SLOT(replyQueryOrder(FoodOrder)));

        i++;
    }
}

UserFoodOrderListWindow::~UserFoodOrderListWindow()
{
    delete ui;
}

//显示个人订单界面
void UserFoodOrderListWindow::replyShowUserFoodOrderListWindow(){
    this->show();
}

//回应查询按钮
void UserFoodOrderListWindow::replyQueryOrder(FoodOrder foodOrder){
    OrderWindow *orderWindow = new OrderWindow();
    orderWindow->init(foodOrder);
    orderWindow->show();
    connect(orderWindow,SIGNAL(dataChange()),this,SLOT(replyDataChange()));
}

//数据改变
void UserFoodOrderListWindow::replyDataChange(){
    this->clear();

    /**
      * @leiteorz
      * 加载订单列表
      */
    FoodOrderTable food_order_table;
    CurrentUser current_user;

    FoodOrder *foodOrder = new FoodOrder[100];
    User cu = current_user.queryCurrentUser();
    foodOrder = food_order_table.queryFoodOrderByAccount(cu.account);   //查询该账户下所有订单

    int i = 0;

    while(foodOrder[i].bill != ""){
        /**
          * @leiteorz
          * 绘制ListItem
          */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(789,89));
        OrderItem *orderItem = new OrderItem();
        orderItem->init(foodOrder[i].id);
        this->setItemWidget(item,orderItem);

        connect(orderItem,SIGNAL(queryOrder(FoodOrder)),this,SLOT(replyQueryOrder(FoodOrder)));

        i++;
    }
}

void UserFoodOrderListWindow::on_freshen_btn_clicked(){
    this->clear();

    /**
      * @leiteorz
      * 加载订单列表
      */
    FoodOrderTable food_order_table;
    CurrentUser current_user;

    FoodOrder *foodOrder = new FoodOrder[100];
    User cu = current_user.queryCurrentUser();
    foodOrder = food_order_table.queryFoodOrderByAccount(cu.account);   //查询该账户下所有订单

    int i = 0;

    while(foodOrder[i].bill != ""){
        /**
          * @leiteorz
          * 绘制ListItem
          */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(789,89));
        OrderItem *orderItem = new OrderItem();
        orderItem->init(foodOrder[i].id);
        this->setItemWidget(item,orderItem);

        connect(orderItem,SIGNAL(queryOrder(FoodOrder)),this,SLOT(replyQueryOrder(FoodOrder)));

        i++;
    }
}
